<template>
  <div>
    <!-- 就弹框出来的列表 优化2：添加数型结构（动态结构）-->
    <el-table size="small " class="aaa"
    :data="menulist" border style="width: 98%" row-key="id" :tree-props="{ children: 'children' }"
      default-expand-all>
      <el-table-column 
       prop="id" label="菜单编号" width="180">
      </el-table-column>
      <el-table-column prop="title" label="菜单名称" width="180">
      </el-table-column>
      <el-table-column prop="pid" label="上级菜单"> </el-table-column>
      <el-table-column label="菜单图标" width="180">
        <template slot-scope="item">
          <div>
            <i :class="item.row.icon"></i>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="url" label="菜单地址" width="180"> </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" icon="el-icon-edit" plain  @click="edit(item.row.id)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" plain @click="del(item.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// 一系列操作 
//辅助性函数
import { mapGetters, mapActions } from "vuex";
//引入封装好的接口
import { menuDelete } from "../../request/api";
export default {
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  mounted() {
    // console.log(this.$store, '仓库')
    this.getMenuList();
  },
  methods: {
    ...mapActions({
      getMenuList: "menu/getMenuListAction",
    }),
    //封装一个删除事件
    del(id) {
      this.$confirm("你确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          menuDelete({
            id,
          }).then((res) => {
            // console.log(res);
            if (res.code == 200) {
              console.log(res);
              this.$message.success(res.msg)
              //重新调用
              this.getMenuList()
            } else {
              this.$message.error(res.msg)
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    edit(id) {
      //告诉父组件打开弹框
      this.$emit('edit', id)
    }
  },
};
</script>

<style scoped>
table{
  border-collapse: collapse;
  width: 100%;
}
.aaa {
        display: block;
        height: 550px;
        overflow-y: scroll;
    }

</style>
